package com.yzg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.pojo.ProductAttribute;
import com.yzg.pojo.ProductAttributeCategory;
import com.yzg.service.ProductAttributeService;
import com.yzg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: decade
 * @Description: 商品分类属性
 * @date: 2021/1/12 23:50
 */
@Api(tags = "ProductAttributeController", description = "商品属性管理")
@RestController
@RequestMapping("/pms/productAttr")
@CrossOrigin
public class ProductAttributeController {

    @Autowired
    private ProductAttributeService productAttributeService;

    @GetMapping("/getAttrListById/{ProductAttrCateId}")
    @ApiOperation(value = "根据商品属性分类id获取所有商品属性列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "当前页大小",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "ProductAttrCateId",value = "商品属性id",required = true,paramType = "Long"),
            @ApiImplicitParam(name = "type",value = "商品属性类型",required = true,paramType = "Integer")
    })
    public R getAttrListById(@PathVariable Long ProductAttrCateId, Integer page, Integer pageSize,Integer type){
        IPage<ProductAttribute> pageParam = new Page<>(page,pageSize);
        List<ProductAttribute> list = productAttributeService.getAttrListById(pageParam,ProductAttrCateId,type);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",pageParam.getTotal());
        return R.ok().data(map);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据商品属性id获取商品属性")
    @ApiImplicitParam(name = "id",value = "商品属性id",required = true,paramType = "Long")
    public R getProductAttrById(@PathVariable Long id){
        ProductAttribute productAttribute = productAttributeService.getProductAttrById(id);
        return R.ok().data("productAttribute",productAttribute);
    }

    @PutMapping("/update")
    @ApiOperation(value = "商品属性编辑")
    @ApiImplicitParam(name = "productAttribute",value = "商品属性信息",required = true,paramType = "ProductAttribute")
    public R update(@RequestBody ProductAttribute productAttribute){
        int row = productAttributeService.updateProductAttributeById(productAttribute);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "商品属性添加")
    @ApiImplicitParam(name = "productAttr",value = "商品属性信息",required = true,paramType = "ProductAttribute")
    public R add(@RequestBody ProductAttribute productAttr){
        int row = productAttributeService.addProductAttr(productAttr);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "商品属性删除")
    @ApiImplicitParam(name = "id",value = "商品属性id",required = true,paramType = "Long")
    public R delete(@PathVariable Long id){
        int row = productAttributeService.deleteProductAttrById(id);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
